package com.atguigu.flink.chapter03_exec1;

import org.apache.flink.api.common.RuntimeExecutionMode;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by Smexy on 2022/10/24
 *   662867,2244074,guangdong,guangzhou,1511658060
 *
 *
 * (henan-5614,1)
 * (zhejiang-36156,1)
 * (guangdong-3611281,2)
 * (zhejiang-7156,2)
 * (shanghai-2244074,1)
 * (hebei-5389,1)
 * (guangdong-2244074,2)
 * (beijing-1715,124)
 * (henan-2914,2)
 * (shanghai-1256540,1)
 * (zhejiang-36237,2)
 * (jiangsu-36156,4)
 * (guangdong-1715,1)
 */
public class Demo5_ProvinceADSClick
{
    public static void main(String[] args) {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setRuntimeMode(RuntimeExecutionMode.BATCH);
        env
            .readTextFile("data/AdClickLog.csv")
            .map(new MapFunction<String, Tuple2<String,Integer>>()
            {
                @Override
                public Tuple2<String, Integer> map(String value) throws Exception {
                    String[] words = value.split(",");
                    return Tuple2.of(words[2].concat("-").concat(words[1]),1);
                }
            })
            .keyBy(new KeySelector<Tuple2<String, Integer>, String>()
            {
                @Override
                public String getKey(Tuple2<String, Integer> value) throws Exception {
                    return value.f0;
                }
            })
            .sum(1)
            .print().setParallelism(1);


        try {
                    env.execute();
                } catch (Exception e) {
                    e.printStackTrace();
                }
    }
}
